<?php

namespace App\Model;

use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\Database\Model\Relations\HasOne;
use Hyperf\HttpServer\Contract\RequestInterface;
use Phper666\JWTAuth\Util\JWTUtil;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use DateTimeZone;
/**
 * App\Distributor
 *
 * @property int $id
 * @property string $name 名称
 * @property string $owner_name 负责人
 * @property string $mobile 负责人手机号
 * @property string $password 密码
 * @property string|null $customer_service_phone 客服电话
 * @property string|null $avatar 头像
 * @property int $rate 分成比例
 * @property float $balance 余额
 * @property float $frozen_balance 冻结余额
 * @property float $total_profit 累计收益
 * @property string $settlement_type 提现类型
 * @property int $total_order 累计订单数
 * @property int $level 等级 最多2级
 * @property int $is_active 是否禁用
 * @property int|null $parent_id 父id
 * @property string|null $service_image 客服二维码
 */
class Distributors extends Model
{
    //
    public bool $timestamps = true;

    protected ?string $table = 'distributors';
    protected array $guarded = [
    ];

    const LEVEL_1 = 1;
    const LEVEL_2 = 2;
    const LEVEL_MAX = 2;
    public static array $levelMap = [
        self::LEVEL_1 => '一级分销',
        self::LEVEL_2 => '二级分销',
    ];


    const SETTLEMENT_TYPE_NO = 'no';
    const SETTLEMENT_TYPE_NOW = 'now';
    const SETTLEMENT_TYPE_ONE_DAY = 'one_day';
    const SETTLEMENT_TYPE_SEVEN_DAY = 'seven_day';
    const SETTLEMENT_TYPE_MONTHLY = 'monthly';

    public static array $settlementTypeMap = [
        self::SETTLEMENT_TYPE_NO => '不结算',
        self::SETTLEMENT_TYPE_NOW => '即时结算',
        self::SETTLEMENT_TYPE_ONE_DAY => 'T + 1',
        self::SETTLEMENT_TYPE_SEVEN_DAY => 'T + 7',
        self::SETTLEMENT_TYPE_MONTHLY => '月结',
    ];

    /**解绑微信
     * @return void
     */
    public function unbindWeChat():void
    {
        $this->openid = null;
        $this->nickname = null;
        $this->save();
    }



    /**构造登陆态内容
     * @param $info
     * @return array
     */
    public function makeInfo($info): array
    {
        $user = [];
        $user['uid'] = $info->id;
        $user['id'] = $info->id;
        $user['name'] = $info->name;
        $user['mobile'] = $info->mobile;
        $user['avatar'] = $info->avatar;
        $user['owner_name'] = $info->owner_name;
        $user['level'] = $info->level;
        $user['rate'] = $info->rate;
        $user['third_account_id'] = $info->third_account_id;
        $user['settle_account_id'] = $info->settle_account_id;
        $user['customer_service_phone'] = $info->customer_service_phone;
        $user['service_image'] = $info->service_image;
        return $user;
    }

    /**获取登陆态
     * @param RequestInterface $request
     * @return array
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface|RedisException
     */
    public function getInfo(RequestInterface $request,): array
    {
        $token = $request->getHeaderLine('Authorization');
        if ($token) {
            $jwtArray = JWTUtil::getUserData("Bearer ".$token);
            if (!isset($jwtArray['id'])) {
                return [];
            }
            $jwtArray['now'] = time();
            if ($jwtArray) {
                $jwtArray['exp'] = $jwtArray['exp']->setTimezone(new DateTimeZone('Asia/Shanghai'));
                $jwtArray['exp_time'] = $jwtArray['exp']->getTimestamp();
                if (time() > $jwtArray['exp_time']) {
                    return [];
                }
            }
            return $jwtArray;
        }
        return [];
    }


    public function station():HasMany{
        return $this->hasMany(Station::class,'distributor_id','id');
    }

    public function stationClass():HasMany{
        return $this->hasMany(StationClass::class,'distributor_id','id');
    }


    public function device():HasMany{
        return $this->hasMany(Device::class, 'distributor_id');
    }

    public function chargeOption():HasMany{
        return $this->hasMany(ChargeOption::class,'distributor_id','id');
    }


    public function deviceProfit():HasMany{
        return $this->hasMany(DistributorDevice::class,'distributor_id','id');
    }


    public function order():HasMany{
        return $this->hasMany(Order::class,'distributor_id','id');
    }

    public function business():HasOne{
        return $this->hasOne(Business::class,'distributor_id','id');
    }

}
